package com.alfayun.fiscalriskanalysis.report;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alfayun.fiscalriskanalysis.common.HttpNotFoundException;
import com.alfayun.fiscalriskanalysis.enterprise.EnterpriseService;
import com.alfayun.fiscalriskanalysis.reportspec.ColumnSpec;
import com.alfayun.fiscalriskanalysis.reportspec.ReportType;
import com.alfayun.fiscalriskanalysis.reportspec.RowSpec;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("/client/report")
public class ReportController {

	@Autowired
	private ReportGroupRepository reportGroupRepository;
	@Autowired
	private ReportRepository reportRepository;
	@Autowired
	private ReportService reportService;
	@Autowired
	private CellService cellService;
	@Autowired
	private EnterpriseService enterpriseService;

	@RequestMapping(value = { "/financial/index/{enterpriseId}", "/financial/index"})
	public String financialIndex(Model model, @PathVariable(required = false) Integer enterpriseId) {
		List<ReportGroup> list = reportGroupRepository.queryReportGroup(enterpriseId, ReportType.FINANCIAL_STATEMENTS);
		model.addAttribute("reportGroupList", list);
		model.addAttribute("enterprises", enterpriseService.list());
		model.addAttribute("enterpriseId", enterpriseId);
		return "client/financial/index";
	}

	@RequestMapping("/financial/list/{groupId}")
	public String financialList(Model Model, @PathVariable("groupId") Integer groupId) {

		List<Report> list = reportRepository.findByReportGroupId(groupId);
		ReportGroup reportGroup = reportGroupRepository.getOne(groupId);

		Model.addAttribute("reportList", list);
		Model.addAttribute("reportGroup", reportGroup);
		return "client/financial/list";
	}

	@RequestMapping("/financial/balanceSheet")
	public String financialBalanceSheet() {
		return "client/financial/balance-sheet";
	}

	@RequestMapping("/financial/reportSheet/{reportName}/{reportId}")
	public String financialReportSheet(Model model, @PathVariable("reportName") String reportName,
			@PathVariable("reportId") Integer reportId) throws HttpNotFoundException {

		try {
			Report report = reportService.queryById(reportId);
			List<RowSpec> rowSpecs = report.reportSpec.template.rows;
			List<ColumnSpec> columnSpecs = report.reportSpec.template.columns;
			model.addAttribute("reportGroup", report.reportGroup);
			model.addAttribute("report", report);
			model.addAttribute("rowSpecs", rowSpecs);
			model.addAttribute("columnSpecs", columnSpecs);
			model.addAttribute("cells", report.getCells());
		} catch (NoSuchReportException e) {
			throw HttpNotFoundException.build(e);
		}
		return "client/financial/" + reportName;
	}

	@PostMapping("/addReport/{reportId}")
	@ResponseBody
	public String addReport(@RequestBody String json, @PathVariable("reportId") Integer reportId)
			throws HttpNotFoundException {
		Report report = null;
		try {
			json = "[" + json + "]";
			ObjectMapper mapper = new ObjectMapper();
			JavaType type = mapper.getTypeFactory().constructParametricType(List.class, Cell.class);

			List<Cell> cells = mapper.readValue(json, type);
			report = cellService.add(reportId, cells);

		} catch (IOException e) {
			e.printStackTrace();
		} catch (NoSuchReportException e) {
			throw HttpNotFoundException.build(e);
		}
		return report.getReportGroup().getId().toString();
	}

	@RequestMapping(value = { "/tax/index/{enterpriseId}", "/tax/index"})
	public String taxIndex(Model model, @PathVariable(required = false) Integer enterpriseId) {
		List<ReportGroup> list = reportGroupRepository.queryReportGroup(enterpriseId, ReportType.ENTERPRISE_INCOME_TAX_RETURN);
		model.addAttribute("reportGroupList", list);
		model.addAttribute("enterprises", enterpriseService.list());
		model.addAttribute("enterpriseId", enterpriseId);
		return "client/tax/index";
	}
	
	@RequestMapping("/tax/list/{groupId}")
	public String taxList(Model Model, @PathVariable("groupId") Integer groupId) {

		List<Report> list = reportRepository.findByReportGroupId(groupId);
		ReportGroup reportGroup = reportGroupRepository.getOne(groupId);

		Model.addAttribute("reportList", list);
		Model.addAttribute("reportGroup", reportGroup);
		return "client/tax/list";
	}
	
	@RequestMapping("/tax/reportSheet/{reportId}")
	public String taxReportSheet(Model model, @PathVariable("reportId") Integer reportId) throws HttpNotFoundException {
		String reportIndex = "";
		try {
			Report report = reportService.queryById(reportId);
			model.addAttribute("report", report);
			reportIndex = report.reportSpec.reportIndex;
		} catch (NoSuchReportException e) {
			throw HttpNotFoundException.build(e);
		}
		return "client/tax/" + reportIndex;
	}
}
